package com.medical.admin.hospital.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.medical.admin.hospital.common.dto.ProjectDto;
import com.medical.admin.hospital.common.vo.ComboVo;
import com.medical.admin.hospital.common.vo.DownMenu;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.common.vo.ProjectVo;
import com.medical.admin.hospital.entity.Combo;
import com.medical.admin.hospital.entity.Project;
import com.medical.admin.hospital.mapper.ComboMapper;
import com.medical.admin.hospital.mapper.ComboProjectMapper;
import com.medical.admin.hospital.mapper.ProjectMapper;
import com.medical.admin.hospital.service.ProjectService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private ComboMapper comboMapper;

    @Resource
    private ComboProjectMapper comboProjectMapper;

    /**
     * 分页查询项目信息
     * @param comboName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<ProjectVo> list(String comboName, int pageNum, int pageSize) {
        if (comboName!=null){
            Combo combo = comboMapper.findByComboName(comboName);
            //通过套餐类型名称查询id
            List<ProjectVo> projectVos = projectMapper.pageList(combo.getComboId(),(pageNum-1)*pageSize,pageSize);
            PageVo<ProjectVo> pageCombos = new PageVo<>();
            pageCombos.setRows(projectVos);
            pageCombos.setTotal(projectVos.size());
            return pageCombos;
        }
        List<ProjectVo> projectVos = projectMapper.pageList(null,(pageNum-1)*pageSize,pageSize);
        PageVo<ProjectVo> pageCombos = new PageVo<>();
        pageCombos.setRows(projectVos);
        pageCombos.setTotal(projectVos.size());
        return pageCombos;
    }

    /**
     * 新增项目信息
     * @param projectDto
     * @return
     */
    @Override
    @Transactional
    public Integer insert(ProjectDto projectDto) {
        //通过项目名称判断套餐是否存在
        Project project = projectMapper.findByProjectName(projectDto.getProjectName());
        if (ObjectUtil.isNotEmpty(project)){
            throw new BaseException(ResponseCode.PROJECT_EXIST_ERROR);
        }

        //转换对象
        project = BeanConvertUtils.convertTo(projectDto, Project::new);

        //新增项目信息
        int count = projectMapper.insert(project);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }

        //通过套餐名称获取id
        ArrayList<Long> ids = new ArrayList<>();
        List<String> comboNames = projectDto.getComboName();
        for (String comboName:comboNames) {
            Combo combo = comboMapper.findByComboName(comboName);
            ids.add(combo.getComboId());
        }

        return projectMapper.insertProjectCombo(project.getProjectId(),ids);
    }

    /**
     * 修改项目信息
     * @param projectDto
     * @return
     */
    @Override
    @Transactional
    public Integer update(ProjectDto projectDto) {
        //通过套餐名称判断套餐是否存在
        Project project = projectMapper.findByProjectName(projectDto.getProjectName());
        Optional.ofNullable(project).orElseThrow(()->new BaseException(ResponseCode.PROJECT_NOT_EXIT_ERROR));

        //转换对象
        Project pj= BeanConvertUtils.convertTo(projectDto, Project::new);

        pj.setProjectId(project.getProjectId());
        //修改套餐信息
        int count = projectMapper.updateById(pj);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }

        //通过项目名称获取id
        ArrayList<Long> ids = new ArrayList<>();
        List<String> comboNames = projectDto.getComboName();
        for (String comboName:comboNames) {
            Combo combo = comboMapper.findByComboName(comboName);
            ids.add(combo.getComboId());
        }

        //删除原先绑定的项目信息，再新增
        Integer integer = comboProjectMapper.deleteByProjectId(pj.getProjectId());
        if (integer<=0){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return comboProjectMapper.insertProjectCombo(project.getProjectId(),ids);
    }



    /**
     * 删除项目信息
     * @param projectId
     * @return
     */
    @Override
    public Integer delete(Long projectId) {
        Project project = projectMapper.selectById(projectId);
        //判断套餐是否存在
        Optional.ofNullable(project).orElseThrow(()->new BaseException(ResponseCode.PROJECT_NOT_EXIT_ERROR));
        //判断套餐是否已删除
        if (project.getState()!=1){
            throw new BaseException(ResponseCode.PROJECT_NOT_EXIT_ERROR);
        }
        project.setState(0);
        int count = projectMapper.updateById(project);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }

    /**
     * 查询项目信息详情
     * @param projectId
     * @return
     */
    @Override
    public ProjectVo select(Long projectId) {
        Project project = projectMapper.selectById(projectId);
        Optional.ofNullable(project).orElseThrow(()-> new BaseException(ResponseCode.PROJECT_NOT_EXIT_ERROR));
        //判断项目状态
        if (project.getState()!=1){
            throw new BaseException(ResponseCode.PROJECT_NOT_EXIT_ERROR);
        }
        return BeanConvertUtils.convertTo(project, ProjectVo::new);
    }

    /**
     * 下拉套餐信息
     * @return
     */
    @Override
    public DownMenu selectComboType() {
        List<Combo> comboList = comboMapper.selectList(new QueryWrapper<>());
        Optional.ofNullable(comboList).orElseThrow(()->new BaseException(ResponseCode.COMBO_NOT_EXIT_ERROR));
        return DownMenu.builder().combos(BeanConvertUtils.convertListTo(comboList, ComboVo::new)).build();
    }
}
